package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.constant.Constant;
import com.ygqh.baby.mapper.YgInformMapper;
import com.ygqh.baby.po.YgInform;
import com.ygqh.baby.po.YgInformExample;
import com.ygqh.baby.po.YgInformUser;
import com.ygqh.baby.service.WxSmallMessageService;
import com.ygqh.baby.service.YgInformService;
import com.ygqh.baby.service.YgInformUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Ss
 */
@Service
public class YgInformServiceImpl implements YgInformService{

	private final static ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(3);
	
	@Autowired
	private YgInformMapper ygInformMapper;
	@Autowired
	private YgInformUserService ygInformUserService;
	@Autowired
	private WxSmallMessageService wxSmallMessageService;
	
	@Override
	public List<YgInform> find() {
		YgInformExample example = new YgInformExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygInformMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgInformExample example = new YgInformExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygInformMapper.countByExample(example);
	}

	@Override
	public YgInform findById(Long id) {
		return ygInformMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgInform ygInform) {
		return ygInformMapper.insertSelective(ygInform);
	}

	@Override
	public int update(YgInform ygInform) {
		return ygInformMapper.updateByPrimaryKeySelective(ygInform);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgInformExample example = new YgInformExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgInform record = new YgInform();
		record.setStatus(DataStatus.Delete.name());
		return ygInformMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgInform ygInform) {
		int r;
		if (ygInform.getId() != null) {
			r = this.update(ygInform);
		} else {
			r = this.save(ygInform);
		}
		return r;
	}

	@Override
	public ResultSet<YgInform> search(QueryInfo queryInfo, String q) {
		int count = ygInformMapper.countSuper(q);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<YgInform> list = ygInformMapper.selectSuper(queryInfo, q);
		return new ResultSet<>(count,list);
	}

	@Override
	public int sendInform(Long userId, String content, InformType informType, String operator) {
		YgInform inform = new YgInform();
		inform.setTitle(informType.getTitle());
		inform.setContent(content);
		inform.setInformType(informType.name());
		inform.setCreateBy(operator);
		inform.setSendTime(new Date());
		inform.setSendStatus("SendSuccess");
		inform.setUpdateBy(operator);
		int row = this.save(inform);

		YgInformUser informUser = new YgInformUser();
		informUser.setInformId(inform.getId());
		informUser.setUserId(userId);
		row += ygInformUserService.save(informUser);

		if (InformType.InviteSuccess.equals(informType)) {
			// 邀请成功通知，发送壹家店小程序模板消息
			wxSmallMessageService.sendInviteUserSuccessMsg(userId, Constant.YIJIADIAN_PLATFORM_NO, content,
					inform.getSendTime());
		}
		return row;
	}

	@Override
	public void asyncSendInform(Long userId, String content, InformType informType, String operator) {
		EXECUTOR_SERVICE.execute(() -> sendInform(userId, content, informType, operator));
	}
}

